package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.CollectOrPrise;
import com.clkj.xiuzheng.pojo.shangcheng.TopicCategory;
import com.clkj.xiuzheng.pojo.shangcheng.TopicOrVideo;
import com.clkj.xiuzheng.service.shangcheng.ScBaiKeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ScBaiKe")
@CrossOrigin
@Api(value = "/ScBaiKe", tags = {"Z商城百科管理"})
public class ScBaiKeController {

    @Autowired
    private ScBaiKeService scBaiKeService;

    @ApiOperation(value = "根据(公司<可选> 获取文章视频集合)")
    @PostMapping("/getTopicOrVideoList")
    public Result getTopicOrVideoList(@RequestParam(required = false) Integer companyId,
                                    @RequestParam Integer pageSize,
                                    @RequestParam Integer pageNum,
                                    @ApiParam(name = "flag", value = "0为文章 1为视频")@RequestParam Integer flag) {
        try {
            PageInfo list = scBaiKeService.getTopicOrVideoList(companyId, pageSize, pageNum,flag);
            return new Result(true, StatusCode.OK, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }



    @ApiOperation(value = "添加视频或文章")
    @PostMapping("/addTopicOrVideo")
    public Result addTopicOrVideo(TopicOrVideo video, HttpServletRequest request) {
        try {
            scBaiKeService.addTopicOrVideo(video,  request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "文章或视频上下架")
    @PostMapping("/updateStatus")
    public Result updateStatus(@ApiParam(name = "ids", value = "文章或视频id集合字符春")@RequestParam String ids,
                               @ApiParam(name = "status", value = "要修改成的状态")@RequestParam Integer status,
                               @ApiParam(name = "flag", value = "0为修改状态，1为置顶")@RequestParam Integer flag) {
        try {
            scBaiKeService.updateStatus(ids,status,flag);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    @ApiOperation(value = "视频或文章分类添加或修改")
    @PostMapping("/addOrUpdateCategory")
    public Result addOrUpdateCategory(TopicCategory category) {
        try {
            scBaiKeService.addOrUpdateCategory(category);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "获取视频或文章分类")
    @PostMapping("/listCategory")
    public Result listCategory(Integer companyId) {
        try {
            List<TopicCategory> categories = scBaiKeService.listCategory(companyId);
            return new Result(true, StatusCode.OK, "修改成功",categories);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "修改视频或文章分类状态")
    @PostMapping("/updateCategoryStatus")
    public Result updateCategoryStatus(@ApiParam(name = "ids", value = "文章或视频id集合字符春")@RequestParam String ids,
                               @ApiParam(name = "status", value = "要修改成的状态")@RequestParam Integer status) {
        try {
             scBaiKeService.updateCategoryStatus(ids,status);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    @ApiOperation(value = "获取文章/视频评论列表")
    @PostMapping("/listComment")
    public Result listComment(@ApiParam(name = "page", value = "页码")@RequestParam Integer page,
                                       @ApiParam(name = "size", value = "条数")@RequestParam Integer size,
                              @ApiParam(name = "topicId", value = "ID")@RequestParam Integer topicId) {
        try {
            PageInfo result = scBaiKeService.listComment(page,size,topicId);
            return new Result(true, StatusCode.OK, "查询成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "获取文章/视频列表(不带分页)")
    @PostMapping("/listAllTopicOrVideo")
    public Result listAllTopicOrVideo() {
        try {
            List<TopicOrVideo> results  = scBaiKeService.listAllTopicOrVideo();
            return new Result(true, StatusCode.OK, "查询成功",results);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "删除文章或视频评论")
    @PostMapping("/deleteComment")
    public Result deleteComment(String ids) {
        try {
            scBaiKeService.deleteComment(ids);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "置顶文章或视频的评论")
    @PostMapping("/setCommentTop")
    public Result setCommentTop(@ApiParam(name = "commentId", value = "评论ID")@RequestParam String ids,
                                @ApiParam(name = "type", value = "0取消1置顶")@RequestParam Integer type) {
        try {
            scBaiKeService.setCommentTop(ids,type);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    @ApiOperation(value = "小程序获取科普首页")
    @PostMapping("/listBaiKe")
    public Result listBaiKe(@ApiParam(name = "categoryId", value = "分账或视频分类ID,首次传0")@RequestParam Integer categoryId,
                            @ApiParam(name = "page", value = "页码")@RequestParam Integer page,
                            @ApiParam(name = "size", value = "每页条数")@RequestParam Integer size) {
        try {
            Map map = scBaiKeService.listBaiKe(categoryId,page,size);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @ApiOperation(value = "小程序获取科普评论")
    @PostMapping("/listBaiKeComment")
    public Result listBaiKeComment(@ApiParam(name = "baiKeId", value = "文章或视频ID")@RequestParam Integer baiKeId,
                                   @ApiParam(name = "userId", value = "用户ID")@RequestParam Integer userId,
                                   @ApiParam(name = "productId", value = "文章或视频关联的商品ID")@RequestParam Integer productId) {
        try {
            Map map = scBaiKeService.listBaiKeComment(baiKeId,userId,productId);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @ApiOperation(value = "小程序获取科普搜索")
    @PostMapping("/listBaiKeSearch")
    public Result listBaiKeSearch(@ApiParam(name = "searchName", value = "搜索信息")@RequestParam String searchName,
                                  @ApiParam(name = "page", value = "页码")@RequestParam Integer page,
                                  @ApiParam(name = "size", value = "条数")@RequestParam Integer size) {
        try {
            PageInfo  map = scBaiKeService.listBaiKeSearch(searchName,page,size);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @ApiOperation(value = "小程序点赞收藏活取消")
    @PostMapping("/addCollectOrPrise")
    public Result addCollectOrPrise(@ApiParam(name = "productId", value = "商品或文章视频")@RequestParam Integer productId,
                                  @ApiParam(name = "userId", value = "用户ID")@RequestParam Integer userId,
                                  @ApiParam(name = "type", value = "0为点赞，1为收藏")@RequestParam Integer type,
                                    @ApiParam(name = "flag", value = "0为商品，1为文章或2频")@RequestParam Integer flag,
                                    @ApiParam(name = "status", value = "1为点赞或收藏，0为取消")@RequestParam Integer status) {
        try {
            scBaiKeService.addCollectOrPrise(productId,userId,type,flag,status);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "小程序点添加科普评论")
    @PostMapping("/addTopicComment")
    public Result addTopicComment(@ApiParam(name = "topicId", value = "商品或文章视频id")@RequestParam Integer topicId,
                                    @ApiParam(name = "userId", value = "用户ID")@RequestParam Integer userId,
                                    @ApiParam(name = "commentDetail", value = "评论内容")@RequestParam String commentDetail,
                                    @ApiParam(name = "parentId", value = "0为以及评论，否则再说")@RequestParam Integer parentId) {
        try {
            scBaiKeService.addTopicComment(topicId,userId,commentDetail,parentId);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }
    @ApiOperation(value = "小程序获取我的收藏")
    @PostMapping("/listCollect")
    public Result listCollect( @ApiParam(name = "userId", value = "用户ID")@RequestParam Integer userId) {
        try {
            List<CollectOrPrise> collect = scBaiKeService.listCollect(userId);
            return new Result(true, StatusCode.OK, "操作成功",collect);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "视频或文章评论导表")
    @PostMapping("/importComment")
    public Result importComment(HttpServletRequest request) {
        try {
            scBaiKeService.importComment(request);
            return new Result(true, StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }
}
